import { getToken} from '@/utils/auth'
import {AccessToken} from '@/utils/token'
import Cookies from 'js-cookie'
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);

const state = {
    access_token: getToken(AccessToken),
    username: '',
    user:null,
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
};

const getters = {
    getAccessToken(state) {
        return state.access_token
    },
    getUser(state) {
        return state.user
    },
    getUserName(){
        return state.name
    },
    getAvatar(){
        return state.avatar
    },
};

const mutations = {
    setAccessToken(state,accessToken){
        if (accessToken == null || accessToken == undefined){
            return
        }else{
            state.access_token = accessToken;
            Cookies.set('AccessToken', accessToken)

        }
    },


    resetAccessToken(state){
        Cookies.remove('AccessToken')
        state.access_token = undefined;
    },

    setUser(state,user){
        state.user = user;
    },

    setUserName(state,userName){
            state.username = userName;
    },


    setAvatar(state,avatar){
        state.avatar = avatar;
    },
};


export default new Vuex.Store({
    state,
    getters,
    mutations,

});
